package com.haixue.android.academy.fragment;

import android.academy.haixue.com.haixue.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.Optional;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link NaviagtionFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link NaviagtionFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NaviagtionFragment extends BaseFragment{
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "currentPosition";
    private static final String ARG_PARAM2 = "totalCount";
    private static final String BACKGROUDRES_PARAM2 = "backgroudRes";
    private static final String IMAGERES_PARAM2 = "imageRes";


    @InjectView(R.id.guide_bkg)
    LinearLayout guide_bkg;

    @InjectView(R.id.image_navigation)
    ImageView image_navigation;

    // TODO: Rename and change types of parameters
    private int currentPosition;
    private int totalCount;
    private int backgroudRes;
    private int imageRes;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param currenrtPosition Parameter 1.
     * @param totalCount Parameter 2.
     * @return A new instance of fragment NaviagtionFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static NaviagtionFragment newInstance(int currenrtPosition, int totalCount,int backgroudRes,int imageRes) {
        NaviagtionFragment fragment = new NaviagtionFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, currenrtPosition);
        args.putInt(ARG_PARAM2, totalCount);
        args.putInt(BACKGROUDRES_PARAM2, backgroudRes);
        args.putInt(IMAGERES_PARAM2, imageRes);
        fragment.setArguments(args);
        return fragment;
    }

    public NaviagtionFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            currentPosition = getArguments().getInt(ARG_PARAM1);
            totalCount = getArguments().getInt(ARG_PARAM2);
            backgroudRes = getArguments().getInt(BACKGROUDRES_PARAM2);
            imageRes = getArguments().getInt(IMAGERES_PARAM2);
        }
    }

    @Override
    protected View getLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (currentPosition == totalCount-1) {
          return   inflater.inflate(R.layout.fragment_naviagtion_last, null);
        }else{
            return   inflater.inflate(R.layout.fragment_naviagtion, null);
        }
    }

    // TODO: Rename method, update argument and hook method into UI event

    @Override
    protected void initDatas() {
        super.initDatas();
        guide_bkg.setBackgroundResource(backgroudRes);
        image_navigation.setImageResource(imageRes);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @OnClick(R.id.bt_login)
    @Optional
    public void doLogin(){
        mListener.onClickLogin();
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onClickLogin();
    }

}
